/**
 * 
 */
package com.haisen.lee.util;

import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import com.haisen.lee.domain.DirectoryItem;
import com.haisen.lee.domain.DirectoryMap;
import com.haisen.lee.exception.DocumentParserException;
import com.haisen.lee.exception.NullValueException;

/**
 * @author Haisen Lee
 * 
 */
public class DomParser {

	/**
	 * 
	 */
	private DomParser() {
		super();
	}

	// TODO finished method to check out
	@SuppressWarnings("unchecked")
	public static DirectoryMap getDirectoryMap()
			throws DocumentParserException, NullValueException {
		URL url = DomParser.class.getResource(SystemParameter
				.getValue(SystemConstant.directoryXMLFile.getSystemConstant()));
		Document document = parse(url);

		// get system parameters to initialize data directory
		Map<String, String> systemParameters = SystemParameter
				.getAllParameters();
		Iterator<Map.Entry<String, String>> iterator = systemParameters
				.entrySet().iterator();
		// initialize data directoryItem
		for (; iterator.hasNext();) {
			Map.Entry<String, String> entry = iterator.next();
			if (entry.getKey() != null && entry.getKey().length() > 0) {
				if (entry.getKey().contains("needParser")) {
					String loadValue = entry.getValue();
					if (loadValue != null && loadValue.length() > 0) {
						List attributeList = document.selectNodes(loadValue);
						DirectoryItem directoryItem = new DirectoryItem();
						for (Iterator iter = attributeList.iterator(); iter
								.hasNext();) {
							Attribute attribute = (Attribute) iter.next();
							String attrValue = attribute.getValue();
							if (SystemConstant.directoryItemIndex
									.getSystemConstant().equals(
											attribute.getName())) {
								directoryItem.setIndex(attrValue);
							} else if (SystemConstant.directoryItemName
									.getSystemConstant().equals(
											attribute.getName())) {
								directoryItem.setItemName(attrValue);
							} else if (SystemConstant.directoryItemTimes
									.getSystemConstant().equals(
											attribute.getName())) {
								directoryItem.setTimes(Long
										.parseLong(attrValue));
							}
						}

						// put directoryItems to DirectoryMap
						List<DirectoryItem> directoryItems = DirectoryMap
								.getDirectoryMap()
								.get(directoryItem.getIndex());
						if (directoryItems == null) {
							directoryItems = new ArrayList<DirectoryItem>();
						} else {
							directoryItems.add(directoryItem);
						}

					} else {
						throw new NullValueException(
								"System parameter value is null");
					}
				}
			}
		}

		return new DirectoryMap();
	}

	private static Document parse(URL url) throws DocumentParserException {
		SAXReader reader = new SAXReader();
		Document document = null;
		try {
			document = reader.read(url);
			return document;
		} catch (DocumentException e) {

			throw new DocumentParserException(e.getMessage(), e);
		}

	}

}
